#ifndef __H_CLIENTBASE_H__
#define __H_CLIENTBASE_H__

#include "CCSocketHandler.h"
#include "Packet.h"
#include "MarkupSTL.h"
#include "GameBase.h"
#include "global.h"
#include "ICC_Timer_Handler.h"
#include "ClientHandler.h"
#include "EncryptDecrypt.h"

class ClientBase: public CCSocketHandler
{
public:
	ClientBase(CGameBase* pbase = NULL) :m_pGameBase(pbase), is_connected(false){ }
    virtual ~ClientBase(){;}

public:
    virtual int OnConnected() 
    {//notice: must be called
        log_error("ClientBase connect [%s:%d]  SUCCESS n",	GetSIP().c_str(),GetPort());
        is_connected=true;

		if (NULL != m_pGameBase)
			m_pGameBase->OnBackConnected(this);

        return 0;
    }
    virtual int OnClose()
    {
        log_error("ClientBase [%s:%d] CLOSE. reconnect_flag:%d \n", GetSIP().c_str(),GetPort(),(GetReconnectFlag()?1:0));
        is_connected=false;

		if (NULL != m_pGameBase)
			return m_pGameBase->OnBackClose(this);

        return 0;
    }
	connType getConnType(void)
	{
		return CONN_TYPE_CONN;
	}
	dataProtocol getDataProtocol(void)
	{
		return DATA_PROTOCOL_TCP;
	}
	void AttachGame(CGameBase* pbase)
	{
		m_pGameBase = pbase;
	}


	virtual int OnPacketComplete(const char * data, int len)
	{
		NETInputPacket input;
		input.Copy(data, len);
		/*uint32 bitflag = input.Getbitflag();
		if(bitflag&g_encodeflag)
		{
			CEncryptDecrypt::DecryptBuffer1(&input);
		}*/
		if (HEART_BEATT_CMD == input.GetCmdType())
		{
			return 0;
		}

		int iret = -1;
		if (NULL != m_pGameBase)
			iret = m_pGameBase->ProccessBackPacket(this, &input);

		if (iret < 0)
		{
			log_error(" Process Packet[0x%x] Failed.", input.GetCmdType());
		}
		return iret;
	}

    virtual ICC_Decoder*  CreateDecoder()
    {
        return new Decoder();
    }
    virtual char* ClientName(){return (char*)"ClientBase";}

    //链接
	int InitConnect(std::string ip, int port, bool isReconnect = true)
	{
		if (ip.empty())
		{
			log_error("ClientBase Error ip NULL\n");
			return -1;
		}

		SetSIP(ip);
		SetPort(port);
		if (isReconnect)
			EnableReconnect();
		else
			DisableReconnect();

		int iret = -1;
		iret = Connect();
		if (iret < 0)
		{
			log_error("ClientBase connect [%s:%d]  ERROR\n", ip.c_str(), port);
			return iret;
		}
		return 0;
	}

    int	InitConnect(char* ip,int port, bool isReconnect = true)
    {
        if(!ip)
        {
            log_error("ClientBase Error ip NULL\n");
            return -1;
        }

        SetSIP(ip);
        SetPort(port);
		if (isReconnect)
			EnableReconnect();
		else
			DisableReconnect();

        int iret=-1;
        iret=Connect();
        if(iret<0)
        {
            log_error("ClientBase connect [%s:%d]  ERROR\n",ip,port);
            return iret;
        }
        return 0;
    }

    int Send(const char * data, int len)
    {
		if (!is_connected)
			return -1;

        int ret=CCSocketHandler::Send(data,len);
        if(ret<0)
        {
            log_error(" Send data error, ret=%d",  ret);
        }
        return ret;
    }

	int Send(CCPacketBase* pOutPkg)
	{
		return Send(pOutPkg->packet_buf(), pOutPkg->packet_size());
	}

    bool IsConnected(){return is_connected;}

public:
	CGameBase* m_pGameBase;
    bool  is_connected;
};



#endif

